﻿namespace ReplayDataFile
{
    using System;
    using System.IO;
    using System.IO.Compression;

    internal class GZipUtil
    {
        public static bool CompressFile(string srcFile, string compressedFile)
        {
            if (!File.Exists(srcFile))
            {
                return false;
            }
            FileStream stream = null;
            FileStream stream2 = null;
            GZipStream stream3 = null;
            try
            {
                stream2 = new FileStream(srcFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                byte[] buffer = new byte[stream2.Length];
                if (stream2.Read(buffer, 0, buffer.Length) != buffer.Length)
                {
                    return false;
                }
                stream = new FileStream(compressedFile, FileMode.Create, FileAccess.ReadWrite);
                stream3 = new GZipStream(stream, CompressionMode.Compress);
                stream3.Write(buffer, 0, buffer.Length);
            }
            catch (Exception)
            {
                return false;
            }
            finally
            {
                if (stream2 != null)
                {
                    stream2.Close();
                }
                if (stream3 != null)
                {
                    stream3.Close();
                }
                if (stream != null)
                {
                    stream.Close();
                }
            }
            return true;
        }

        public static bool DecompressFile(string srcFile, string decompressedFile)
        {
            FileStream stream = null;
            GZipStream stream2 = null;
            FileStream stream3 = null;
            try
            {
                int num2;
                bool flag2;
                stream = new FileStream(srcFile, FileMode.Open, FileAccess.Read, FileShare.None);
                stream2 = new GZipStream(stream, CompressionMode.Decompress);
                stream3 = new FileStream(decompressedFile, FileMode.Create, FileAccess.ReadWrite);
                int num = 0;
                byte[] buffer = new byte[0x8000];
                goto Label_0065;
            Label_0033:
                num2 = stream2.Read(buffer, 0, buffer.Length);
                if (num2 == 0)
                {
                    goto Label_00AD;
                }
                stream3.Write(buffer, 0, num2);
                num += num2;
            Label_0065:
                flag2 = true;
                goto Label_0033;
            }
            catch (Exception)
            {
                return false;
            }
            finally
            {
                if (stream2 != null)
                {
                    stream2.Close();
                }
                if (stream != null)
                {
                    stream.Close();
                }
                if (stream != null)
                {
                    stream3.Close();
                }
            }
        Label_00AD:
            return true;
        }

        public static byte[] UnzipBuffer(byte[] inBuff)
        {
            int num = inBuff[0] + (inBuff[1] * 0x100);
            byte[] dst = new byte[inBuff.Length - 2];
            Buffer.BlockCopy(inBuff, 2, dst, 0, dst.Length);
            GZipStream stream = new GZipStream(new MemoryStream(dst), CompressionMode.Decompress, true);
            byte[] buffer = new byte[num];
            stream.Read(buffer, 0, buffer.Length);
            return buffer;
        }

        public static byte[] ZipBuffer(byte[] inBuff)
        {
            MemoryStream stream = new MemoryStream();
            new GZipStream(stream, CompressionMode.Compress).Write(inBuff, 0, inBuff.Length);
            int position = (int) stream.Position;
            if ((position + 2) >= inBuff.Length)
            {
                return inBuff;
            }
            byte[] buffer = new byte[position + 2];
            buffer[0] = (byte) (inBuff.Length & 0xff);
            buffer[1] = (byte) ((inBuff.Length >> 8) & 0xff);
            stream.Position = 0L;
            stream.Read(buffer, 2, position);
            return buffer;
        }
    }
}

